package com.tougutips.modules.user.followers.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.tougutips.modules.user.followers.entity.Followers;
import com.tougutips.modules.user.followers.entity.User;
import com.tougutips.modules.user.followers.mapper.UserMapper;
import com.tougutips.modules.user.followers.service.FollowersService;
import com.tougutips.modules.user.followers.mapper.FollowersMapper;
import com.tougutips.modules.user.followers.service.RedisFollowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author kkd
* @description 针对表【followers】的数据库操作Service实现
* @createDate 2022-07-21 19:08:44
*/

@Service
public class FollowersServiceImpl extends ServiceImpl<FollowersMapper, Followers>
    implements FollowersService{
    @Autowired
    FollowersMapper followersMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    RedisFollowService redisFollowService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveRedis2DB() {
        List<Followers> followersList = redisFollowService.getFollowDataFromRedis();
        for (Followers follow : followersList) {
            //如果无记录则直接存入DB
            Followers followers = findFollowerById(follow.getFollowersId(),follow.getBeFollowedId());
            if( followers == null){
                save(follow);
                //有记录则设置status，存入DB
            }else{
                followers.setStatus(follow.getStatus());
                save(followers);
            }
        }
    }

    @Override
    public AjaxResult getRelateFollowers(Long userId, Long otherUserId) {
        return null;
    }

    @Override
    public Followers findFollowerById(Long userId, Long otherUserId) {
        LambdaQueryWrapper<Followers> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Followers::getFollowersId,userId).eq(Followers::getBeFollowedId,otherUserId);
        return followersMapper.selectOne(lqw);
    }

}




